/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rowan.infovis.soccer;

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author Kevin McGarry
 */
public class Team {

    private String name;
    private ArrayList<PlayerData> players = new ArrayList<PlayerData>();
    private ArrayList<String> playerNames = new ArrayList<String>();
    private PlayerData currentPlayer;
    private PlayerData currentSecondPlayer;

    public Team(String n) {
        this.name = n;
    }

    public void addPlayer( String name ) {
        if( !playerNames.contains(name) ) {
            playerNames.add(name);
        }
    }

    public void sortPlayers() {
        Collections.sort(playerNames);

        for(int i=0; i<players.size(); i++){
            if(playerNames.get(0).compareTo(players.get(i).getLastName()) == 0) {
                currentPlayer = players.get(i);
                currentSecondPlayer = players.get(i);
            }
        }
    }

    public String getName() {
        return name;
    }

    public void addPlayer(String fn, String ln) {
        PlayerData p = new PlayerData(fn, ln);
        players.add(p);
        
        currentPlayer = players.get(players.size()-1);
        currentSecondPlayer = players.get(players.size()-1);

        playerNames.add(ln);
    }

    public void setCurrentPlayer(String ln) {
        for(int i=0; i<players.size(); i++) {
            if (players.get(i).getLastName().compareTo(ln) == 0) {
                currentPlayer = players.get(i);
                break;
            }

        }
    }

    public void setSecondCurrentPlayer(String ln) {
        for(int i=0; i<players.size(); i++) {
            if (players.get(i).getLastName().compareTo(ln) == 0) {
                currentSecondPlayer = players.get(i);
                break;
            }

        }
    }

    public PlayerData getCurrentPlayer() {
        return currentPlayer;
    }

    public PlayerData getSecondCurrentPlayer() {
        return currentSecondPlayer;
    }

    public ArrayList<String> getPlayerNames(String letter) {
        ArrayList<String> playerNameByLetter = new ArrayList<String>();
        for(int i=0; i<playerNames.size(); i++) {
            String firstLetter = playerNames.get(i).substring(0,1);
            if(letter.compareTo(firstLetter)==0) {
                playerNameByLetter.add(playerNames.get(i));
            }
        }
        return playerNameByLetter;
    }
}
